var express = require('express');
var router = express.Router();
var db = require('../../mysqldb');

module.exports = function (app) {
    app.use('/api', router);
};
//首页
    //轮播图
    router.post('/banner',(req,res,next)=>{
        db("select * from Banner where IsDisplay=1 and IsIndex=1 order by Sort desc",(err,rows)=>{
            if(err){
                res.send({ret:false,err:err})
            }
            var offset= req.body.offset?req.body.offset:0;
            var limit = req.body.limit?req.body.limit:10;
            var newrows =rows.splice(offset,limit);
            res.send({
                ret:true,
                data:newrows
            })
        })
    })
    //首页所有新闻
    router.post('/newsindex',(req,res,next)=>{
        db(`select * from News where (FrId=${req.body.FrId} or SecondFrId=${req.body.FrId}) and IsDisplay=1 and IsIndex=1 order by Sort desc`,(err,rows)=>{
            if(err){
                res.send({ret:false,err:err})
            }
            var offset= req.body.offset?req.body.offset:0;
            var limit = req.body.limit?req.body.limit:10;
            var newrows =rows.splice(offset,limit);
            res.send({ ret:true,data:newrows })
        })
    })
    //获取协同单位
    router.post('/xietong',(req,res,next)=>{
        db("select * from AddList where IsDisplay=1 and IsIndex=1 order by Sort desc",(err,rows)=>{
            if(err){
                res.send({ret:false})
              }
              var newdata=[[],[],[],[],[],[],[]];
              rows.forEach((item,index)=>{
                  if(item.Type == 2){
                    newdata[item.FrId-7].push({
                        a:item,
                        b:[]
                    })
                  }
              })
              newdata.forEach((item1,index1)=>{
                    item1.forEach((item2,index2)=>{
                        rows.forEach((item,index)=>{
                            if(item.Type==3){
                                if(item.FrId == item2.a.id){
                                    newdata[index1][index2].b.push(item)
                                }
                            }
                        })
                    })
              })
              var datas = newdata[1]
              var arrs = []
              datas.forEach((item,index)=>{
                arrs.push(item.a.id)
                if(item.b.length>0){
                    item.b.forEach((item1,index1)=>{
                        arrs.push(item1.id)
                    })
                }
              })
              //console.log(arrs)
              var newsql = ''
              arrs.forEach((item,index)=>{
                  if(index ==arrs.length-1){
                    newsql+='FrId='+item
                  }else{
                    newsql+='FrId='+item +' or '
                  }
                  
              })
              db(`select * from News where (${newsql}) and IsDisplay=1 and IsIndex=1 order by Sort desc`,(err,rows)=>{
                if(err){
                    res.send({ret:false})
                  }
                  var offset= req.body.offset?req.body.offset:0;
                  var limit = req.body.limit?req.body.limit:10;
                  var newrows =rows.splice(offset,limit);
                  res.send({ret:true,data:newrows})
              })
         });
    })
    //展示下拉二级addList
    router.post('/addlist',(req,res,next)=>{
        db("select * from AddList where IsDisplay=1 and IsIndex=1 order by Sort desc",(err,rows)=>{
            if(err){
                res.send({ret:false})
              }
              var newdata=[[],[],[],[],[],[],[]];
              rows.forEach((item,index)=>{
                  if(item.Type == 2){
                    newdata[item.FrId-7].push({
                        a:item,
                        b:[]
                    })
                  }
              })
              newdata.forEach((item1,index1)=>{
                    item1.forEach((item2,index2)=>{
                        rows.forEach((item,index)=>{
                            if(item.Type==3){
                                if(item.FrId == item2.a.id){
                                    newdata[index1][index2].b.push(item)
                                }
                            }
                        })
                    })
              })
              if(req.body.ftype || req.body.ftype== '0'){
                res.send({ret:true,data:newdata[req.body.ftype]})
              }else{
                res.send({ret:true,data:newdata})
              }
              
            });
    })
//二级页
    //单个二级列
    router.post('/addlistOne',(req,res,next)=>{
        db("select * from AddList where IsDisplay=1 order by Sort desc",(err,rows)=>{
            if(err){
                res.send({ret:false})
              }
              var newdata=[[],[],[],[],[],[],[]];
              rows.forEach((item,index)=>{
                  if(item.Type == 2){
                    newdata[item.FrId-7].push({
                        a:item,
                        b:[]
                    })
                  }
              })
              newdata.forEach((item1,index1)=>{
                    item1.forEach((item2,index2)=>{
                        rows.forEach((item,index)=>{
                            if(item.Type==3){
                                if(item.FrId == item2.a.id){
                                    newdata[index1][index2].b.push(item)
                                }
                            }
                        })
                    })
              })
              if(req.body.ftype || req.body.ftype == 0){
                res.send({ret:true,data:newdata[req.body.ftype]})
              }else{
                res.send({ret:true,data:newdata})
              }
              
            });
    })
    //查看新闻列表
    router.post('/newslist',(req,res,next)=>{
        db(`select * from News where (FrId=${req.body.FrId} or SecondFrId=${req.body.FrId}) and IsDisplay=1  order by Sort desc`,(err,rows)=>{
            if(err){
                res.send({ret:false,err:err})
            }
            var offset= req.body.offset?req.body.offset:0;
            var limit = req.body.limit?req.body.limit:10;
            var total = rows.length;
            var newrows =rows.splice(offset,limit);
            res.send({
                ret:true,
                data:newrows,
                total
            })
        })
    })
    //查看新闻详情
    router.post('/detail',(req,res,next)=>{
        db(`select * from News where id=${req.body.id} and IsDisplay=1`,(err,rows)=>{
            if(err){
                res.send({ret:false})
              }
              res.send({ret:true,data:rows[0]})
        })
    })
    //搜索细化版本
    router.post('/search',(req,res,next)=>{
        db(`select * from News where (Title like '%${req.body.Title}%') and IsDisplay=1  order by Sort desc`,(err,rows)=>{
            if(err){
                res.send({ret:false,err:err})
            }
            var offset= req.body.offset?req.body.offset:0;
            var limit = req.body.limit?req.body.limit:10;
            var total = rows.length;
            var newrows =rows.splice(offset,limit);
            res.send({
                ret:true,
                data:newrows,
                total
            })
        })
    })
